package com.zj.mes.login.controller;

import com.zj.mes.common.controller.BaseController;
import com.zj.mes.common.entity.ResultsModel;
import com.zj.mes.login.entity.UpdatePassword;
import com.zj.mes.login.entity.User;
import com.zj.mes.login.service.ILoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: 王晓楠
 * @Date: 2019/4/28 14:25
 * @Version 1.0
 */
@Controller
@RequestMapping("/log")
public class LoginController extends BaseController {
    @Autowired
    private ILoginService loginService;

    /**
     * 获取登录用户信息
     * @return
     */
    @RequestMapping("/getLogin")
    @ResponseBody
    public String getLogin(){
        String shiroUserName =getShiroUserName();
        String login = loginService.getLogin(shiroUserName);
        return login;
    }
    /**
     * 获得当前用户姓名
     * @return 用户名
     */
    @RequestMapping("/getEmployeeName")
    @ResponseBody
    public String getEmployeeName(){
        return loginService.getEmployeeName();
    }
    /**
     * 登录页面跳转
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public ResultsModel login(@RequestBody User user){
        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        try {
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.getLoginName(),user.getPassword());
        //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
            //return "index";
            return returnSuccessInfo("登录成功！");
        } catch (UnknownAccountException e) {
           // throw new RuntimeException("账号不存在！", e);
            return returnErrorInfo("账号不存在！");
        }catch(NullPointerException e){
            return returnErrorInfo("账号不存在！");
        } catch (DisabledAccountException e) {
            //throw new RuntimeException("账号未启用！", e);
            return returnErrorInfo("账号未启用！");
        } catch (IncorrectCredentialsException e) {
            //throw new RuntimeException("密码错误！", e);
            return returnErrorInfo("密码错误！");
        } catch (Throwable e) {
            return  returnErrorInfo(e.getMessage());
        }

    }

    /**
     * 登陆成功页面跳转
     * @return
     */
    @RequestMapping(value = "/toindex")
    public String toindex(){
        return "index.html";
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login.html";
    }

    /**
     * 修改密码
     * @param pw
     * @return
     */
    @RequestMapping(value = "/updatePw")
    @ResponseBody
    public ResultsModel updatePw(@RequestBody UpdatePassword pw){
        String shiroUserName =getShiroUserName();
        pw.setLoginname(shiroUserName);
        int i = loginService.updatePw(pw);
        if(i==1){
            return returnSuccessInfo("更改成功,请重新登录");
        }else{
            return returnErrorInfo("原始密码错误");
        }
    }

    /**
     * 数据初始化
     * @param map
     * @return
     */
    @RequestMapping(value = "/addUser")
    public String addUser(@RequestBody User map){
        User user = loginService.addUser(map);
        return "addUser is ok! \n" + user;
    }

    //角色认证
    @RequiresRoles("admin")

    @RequestMapping(value = "/create")
    public String create(){
        return "Create success!";
    }
    @RequestMapping("/tojuese")
    public String tojuese(){
        return "juese";
    }
    @RequestMapping("/toziyuan")
    public String toziyuan(){
        return "ziyuan";
    }

    /**
     * 获取用户所拥有的权限
     * @return
     */
    @RequestMapping("/getPowerByUsername")
    @ResponseBody
    public List<Map> getPowerByUsername(@RequestBody Map map){

        int id = Integer.parseInt(map.get("id").toString());
        return loginService.getPowerByUsername(id);
    }

    /**
     * 获取用户修改后的权限
     * @param map
     * @return
     */
    @RequestMapping("/submitPower")
    @ResponseBody
    public String submitPower(@RequestBody Map map){
        List list = (List) map.get("id");
        int roleId = (int) map.get("role");
        loginService.submitPower(list,roleId);
        return "zj";
    }
    /**
     * 获取角色
     */
    @RequestMapping("/getRole")
    @ResponseBody
    public List<Map>  getRole(){
       return loginService.power();
    }

}
